package com.hyts.standard.webflux.web;

import com.hyts.standard.webflux.client.HttpRequestClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;

import java.time.Duration;

/**
 * project-name:standard-webflux
 * package-name:com.hyts.standard.webflux.controller
 * author:Libo/Alex
 * create-date:2019-06-19 10:40
 * email:liboware@gmail.com
 * description:此类主要用于:
 */
@RestController
public class BaseController {

    @Autowired
    HttpRequestClient httpRequestClient;

    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/baidu")
    public Mono<String> baidu() {
        return httpRequestClient.get("http://www.baidu.com/");
    }


    @GetMapping("/hello")
    public Mono<String> hello() {
        return Mono.just("Welcome to reactive world ~")
                .delayElement(Duration.ofMillis(1000));
    }
    @GetMapping("/hello2")
    public Mono<String> hello2() {
        String htmlContent = restTemplate.getForObject("http://www.baidu.com", String.class);
        return Mono.fromCallable(() -> htmlContent)
                .subscribeOn(Schedulers.elastic());
    }
}
